
 /**
 * ParametersTraceFilter.java.java.
 * User: BrianChen Date: 2012-7-29 下午01:16:54
 *
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
 * All rights reserved.
 */
package com.collapsar.core.aop.filter;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.collapsar.core.util.web.RequestUtils;

/**
 * <pre>
 * REVISIONS: 
 * Version 	Date 		Author 			Description
 * ------------------------------------------------------------------- 
 * 1.0 		2012-7-29 	BrianChen 	1. Created this class. 
 * </pre>
 * 
 * @author BrianChen
 * @date 2012-7-29 下午01:16:54
 * @version 1.0
 * @see
 * 
 */
public class ParametersTraceFilter implements Filter{
	private static final Logger logger = LoggerFactory.getLogger(ParametersTraceFilter.class);
	
	
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub 
	}
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub 
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest hreq = (HttpServletRequest) request;
		String requestURL = hreq.getRequestURL().toString();

		StringBuilder reqParams = new StringBuilder();
		reqParams.append("###Request URI:").append(requestURL).append("\n");

		Map<String,Object> paramsMap = RequestUtils.getQueryParams(hreq);
		for(Entry<String, Object> entry : paramsMap.entrySet()){
			reqParams.append(entry.getKey()).append(":").append(entry.getValue()).append("\n");
		}
		logger.info(reqParams.toString());

		HttpSession session = hreq.getSession();
		Enumeration<String> sesEnu = session.getAttributeNames();
		StringBuilder sesParams = new StringBuilder();
		sesParams.append("###Session:\n");
		while (sesEnu.hasMoreElements()) {
			String param = (String) sesEnu.nextElement();
			sesParams.append("\t # param=").append(param).append(" value=").append(session.getAttribute(param)).append("\n");
		}
		logger.info(sesParams.toString());

		// Pass control on to the next filter
		chain.doFilter(request, response);
	}


}

